package com.example.controller;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.example.listenanddriveproject.R;

public class CircleFragment extends Fragment {
	
	private Context context;
	private View rootView;
	private SeekBar seekBarCercle;
	private TextView textCercle;
	private TextView textSon;
	private SeekBar seekBarSon;
	private Button button;
	private Button buttonReset;
	
	public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		
		rootView = inflater.inflate(R.layout.fragment_cercle, container, false);
		
		context = this.getActivity();
		
		textCercle = (TextView) rootView.findViewById(R.id.textViewCercle);
		textCercle.setText(String.valueOf(MainActivity.settings.getCercleSound().getCercle()));
		
		seekBarCercle=(SeekBar) rootView.findViewById(R.id.seekBarCercle);
		seekBarCercle.setProgress(MainActivity.settings.getCercleSound().getCercle());
		seekBarCercle.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		    	textCercle.setText(String.valueOf(progress));
		    	MainActivity.settings.getCercleSound().setCercle(progress);
		    }       
	    });
		
		textSon = (TextView) rootView.findViewById(R.id.textViewSon);
		textSon.setText(String.valueOf(MainActivity.settings.getCercleSound().getSound()));
	    
	    seekBarSon=(SeekBar) rootView.findViewById(R.id.seekBarSon);
	    seekBarSon.setProgress(MainActivity.settings.getCercleSound().getSound());
	    seekBarSon.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		    	textSon.setText(String.valueOf(progress));
		    	MainActivity.settings.getCercleSound().setSound(progress);
		    }       
	    });
		
		button = (Button) rootView.findViewById(R.id.apply1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                MainActivity.settings.saveArray(context);
            }
        });
        
        buttonReset = (Button) rootView.findViewById(R.id.buttonReset);
        buttonReset.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                MainActivity.settings.resetSettings(context);
                refresh();
            }
        });
        
		return rootView;
	}
	
	/***
	 * refresh the UI when the reset is called
	 */
	public void refresh(){
		
		seekBarCercle.setProgress(MainActivity.settings.getCercleSound().getCercle());
		textCercle.setText(String.valueOf(MainActivity.settings.getCercleSound().getCercle()));
			
		seekBarSon.setProgress(MainActivity.settings.getCercleSound().getSound());
		textSon.setText(String.valueOf(MainActivity.settings.getCercleSound().getSound()));
	}

}
